home *** CD-ROM | disk | FTP | other *** search
/ Light ROM 1 / LIGHT-ROM 1 (Amiga Library Services)(1994).iso / ffdisks / d945.lha / Reminder / src / src.lha / Gadutil.c < prev    next >
C/C++ Source or Header  |  1993-04-19  |  11KB  |  387 lines

  1. /* Gadget utilities for Reminder */
  2.  
  3. /* $Id: Gadutil.c,v 1.8 1993/04/18 21:53:23 Matti_Rintala Exp $ */
  4.  
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <ctype.h>
  9. #include <errno.h>
  10. #include <time.h>
  11.  
  12. #include <intuition/intuition.h>
  13. #include <intuition/gadgetclass.h>
  14.  
  15. #include <dos/dos.h>
  16.  
  17. #include <libraries/gadtools.h>
  18.  
  19. #ifdef __SASC
  20. #include <proto/intuition.h>
  21. #include <proto/gadtools.h>
  22. #include <proto/dos.h>
  23. #endif
  24.  
  25. #ifdef _DCC
  26. #include <clib/intuition_protos.h>
  27. #include <clib/gadtools_protos.h>
  28. #include <clib/dos_protos.h>
  29.  
  30. /* DICE does not have difftime(), although its prototype is in time.h. This is
  31.    a bug in DICE, so we'll have to declare difftime() as macro */
  32. #define difftime(a,b) ((double)(a) - (double)(b))
  33. #endif
  34.  
  35.  
  36. #include "Gadfunc.h"
  37. #include "Globals.h"
  38. #include "CalcDate.h"
  39. #include "Gadutil.h"
  40.  
  41. static int matchstr(const char *matched, const char *str);
  42.  
  43. /* CheckInt checks that the contents of an integer gadget is valid and undos
  44.    the contents if it is not. If data is valid, it is stored into a given
  45.    integer variable. */
  46.  
  47. int CheckInt(struct Gadget *gad, int minval, int maxval, short *var) {
  48.  
  49.   struct StringInfo *strinf;    /* pointer to gadgets StringInfo */
  50.  
  51.   strinf = (struct StringInfo *)gad->SpecialInfo;
  52.  
  53.   /* Check if the value is out of range */
  54.   if (*strinf->Buffer == '\0') {
  55.     /* Null entry is valid, marked with 0 */
  56.     *var = 0;
  57.     return 0;
  58.   }
  59.  
  60.   if (strinf->LongInt < minval || strinf->LongInt > maxval) {
  61.     /* Out of range, so put old value back */
  62.     /* SetInt(gad, *var); */
  63.     DisplayBeep(NULL);
  64.     return 1;            /* No valid data found */
  65.   }
  66.   else {
  67.     /* Set the new value to variable */
  68.     *var = strinf->LongInt;
  69.     return 0;            /* Valid data found */
  70.   }
  71. }
  72.  
  73. /* CheckMonth checks that the month value entered by user is correct.
  74.    It sets the string gadget to new month value selected from the list. */
  75. int CheckMonth(void) {
  76.  
  77.   struct Gadget *gad = MainGadgets[GDX_Month];
  78.   struct StringInfo *strinf = (struct StringInfo *)gad->SpecialInfo;
  79.   char *tail;
  80.   int mnth;
  81.   struct Node *node; /* Month names */
  82.  
  83.   /* If string is empty, it is valid */
  84.   if (*(strinf->Buffer) == '\0') {
  85.     mnth = 0;
  86.   }
  87.   else {
  88.     /* Check if the value entered is a number */
  89.     mnth = strtol(strinf->Buffer, &tail, 10);
  90.     if (mnth < 1 || mnth > 12 || *tail != '\0') {
  91.       /* If it is not a valid number, check if it is a month name */
  92.       for (mnth = 1, node = (struct Node *)MonthList0List.mlh_Head;
  93.        node != (struct Node *)MonthList0List.mlh_Tail;
  94.        mnth++, node = node->ln_Succ) {
  95.  
  96.     /* If name checks, exit loop */
  97.     if (matchstr(strinf->Buffer, node->ln_Name))
  98.       break;
  99.       }
  100.       /* If no match found, set previous value and fail */
  101.       if (node == (struct Node *)MonthList0List.mlh_Tail) {
  102.     /* SetMonth(month); */
  103.     DisplayBeep(NULL);
  104.     return 1;
  105.       }
  106.  
  107.       /* Check for ANY */
  108.       if (mnth == 13) mnth = 0;
  109.  
  110.     }
  111.   }
  112.   /* Update string gadget contents */
  113.   SetMonth(mnth);
  114.  
  115.   /* Update global month-variable */
  116.   month = mnth;
  117.  
  118.   return 0;
  119. }
  120.  
  121. /* CheckText checks the text (not currently) and stores it into global variable */
  122. int CheckText(void) {
  123.  
  124.   struct StringInfo *strinf = (struct StringInfo *)MainGadgets[GDX_Text]->SpecialInfo;
  125.  
  126.   /* Simply copy the text */
  127.   strncpy(text, strinf->Buffer, TEXTLEN);
  128.  
  129.   return 0;
  130. }
  131.  
  132. /* CheckPort checks the port (not currently) and stores it into global variable */
  133. int CheckPort(void) {
  134.  
  135.   struct StringInfo *strinf =
  136.     (struct StringInfo *)MainGadgets[GDX_ARexxPort]->SpecialInfo;
  137.  
  138.   /* Simply copy the text */
  139.   strncpy(arexxport, strinf->Buffer, AREXXPORTLEN);
  140.  
  141.   return 0;
  142. }
  143.  
  144. /* CheckCom checks the com (not currently) and stores it into global variable */
  145. int CheckCom(void) {
  146.  
  147.   struct StringInfo *strinf =
  148.     (struct StringInfo *)MainGadgets[GDX_ARexxCom]->SpecialInfo;
  149.  
  150.   /* Simply copy the text */
  151.   strncpy(arexxcom, strinf->Buffer, AREXXCOMLEN);
  152.  
  153.   return 0;
  154. }
  155.  
  156. /* SetInt sets an integer gadget to new value */
  157. void SetInt(struct Gadget *gad, short newval) {
  158.  
  159.   struct StringInfo *strinf;
  160.  
  161.   /* Get the gadgets StringInfo */
  162.   strinf = (struct StringInfo *)gad->SpecialInfo;
  163.  
  164.   RemoveGList(MainWnd, gad, 1);    /* Remove the gadget from window */
  165.   if (newval == 0) {
  166.     /* Value 0 is represented with empty gadget */
  167.     *strinf->Buffer = '\0';
  168.   }
  169.   else {
  170.     sprintf(strinf->Buffer, "%d", newval); /* Put the new value to gadget */
  171.     strinf->LongInt = newval;
  172.     strinf->BufferPos = 0;
  173.     strinf->DispPos = 0;
  174.   }
  175.   AddGList(MainWnd, gad, ~0, 1, NULL); /* Put gadget back to window */
  176.   RefreshGList(gad, MainWnd, NULL, 1); /* Refresh the gadget */
  177. }
  178.  
  179. /* SetYear sets the Year gadget to new value */
  180. void SetYear(short newval) {
  181.  
  182.   SetInt(MainGadgets[GDX_Year], newval);
  183. }
  184.  
  185. /* SetDay sets the Day gadget to new value */
  186. void SetDay(short newval) {
  187.  
  188.   SetInt(MainGadgets[GDX_Day], newval);
  189. }
  190.  
  191. /* SetBefore sets the Before gadget to new value */
  192. void SetBefore(short newval) {
  193.  
  194.   SetInt(MainGadgets[GDX_Before], newval);
  195. }
  196.  
  197. /* SetAfter sets the After gadget to new value */
  198. void SetAfter(short newval) {
  199.  
  200.   SetInt(MainGadgets[GDX_After], newval);
  201. }
  202.  
  203. /* SetWDay sets the Weekday gadget to given value */
  204. void SetWDay(short newval) {
  205.  
  206.   struct Gadget *gad = MainGadgets[GDX_Weekday];
  207.  
  208.   newval = (newval != 0) ? (newval-1) : 7;
  209.  
  210.   /* Update the selection of the Weekday gadget */
  211.   GT_SetGadgetAttrs(gad, MainWnd, NULL, GTLV_Selected, (Tag)newval, TAG_DONE);
  212. }
  213.  
  214. /* SetMonth sets the Month gadget to given value */
  215. void SetMonth(short newval) {
  216.  
  217.   struct Gadget *gad = MainGadgets[GDX_MonthList];
  218.  
  219.   /* Calculate correct index for gadget */
  220.   newval = (newval != 0) ? (newval-1) : 12;
  221.   /* Update the selection of the Month gadget */
  222.   GT_SetGadgetAttrs(gad, MainWnd, NULL, GTLV_Selected, (Tag)newval, TAG_DONE);
  223. }
  224.  
  225. /* SetAutodelete sets the Autodelete checkbox to given value */
  226. void SetAutodelete(BOOL newval) {
  227.  
  228.   struct Gadget *gad = MainGadgets[GDX_Autodelete];
  229.  
  230.   /* Update the checkbox state */
  231.   GT_SetGadgetAttrs(gad, MainWnd, NULL, GTCB_Checked, (Tag)newval, TAG_DONE);
  232. }
  233.  
  234. /* SetGrouped sets the Grouped checkbox to given value */
  235. void SetGrouped(BOOL newval) {
  236.  
  237.   struct Gadget *gad = MainGadgets[GDX_Grouped];
  238.  
  239.   /* Update the checkbox state */
  240.   GT_SetGadgetAttrs(gad, MainWnd, NULL, GTCB_Checked, (Tag)newval, TAG_DONE);
  241. }
  242.  
  243. /* SetText sets a new value for text gadget */
  244. void SetText(const char *newtext) {
  245.  
  246.   struct Gadget *gad = MainGadgets[GDX_Text];
  247.  
  248.   /* Update the gadget */
  249.   GT_SetGadgetAttrs(gad, MainWnd, NULL, GTST_String, (Tag)newtext, TAG_DONE);
  250. }
  251.  
  252. /* SetPort sets a new value for ARexxPort gadget */
  253. void SetPort(const char *newtext) {
  254.  
  255.   struct Gadget *gad = MainGadgets[GDX_ARexxPort];
  256.  
  257.   /* Update the gadget */
  258.   GT_SetGadgetAttrs(gad, MainWnd, NULL, GTST_String, (Tag)newtext, TAG_DONE);
  259. }
  260.  
  261. /* SetCom sets a new value for ARexxCom gadget */
  262. void SetCom(const char *newtext) {
  263.  
  264.   struct Gadget *gad = MainGadgets[GDX_ARexxCom];
  265.  
  266.   /* Update the gadget */
  267.   GT_SetGadgetAttrs(gad, MainWnd, NULL, GTST_String, (Tag)newtext, TAG_DONE);
  268. }
  269.  
  270. /* SetAMode sets a new value for ARexxMode gadget */
  271. void SetAMode(char newmode) {
  272.  
  273.   struct Gadget *gad = MainGadgets[GDX_ARexxMode];
  274.  
  275.   /* Update the gadget */
  276.   GT_SetGadgetAttrs(gad, MainWnd, NULL, GTMX_Active, (Tag)newmode, TAG_DONE);
  277.  
  278.   /* (De)select other ARexx gadgets depending on mode */
  279.   GT_SetGadgetAttrs(MainGadgets[GDX_ARexxCom], MainWnd, NULL,
  280.             GA_Disabled, (Tag)(newmode == AREXXNMODE), TAG_DONE);
  281.   GT_SetGadgetAttrs(MainGadgets[GDX_ARexxPort], MainWnd, NULL,
  282.             GA_Disabled, (Tag)(newmode != AREXXCMODE), TAG_DONE);
  283. }
  284.  
  285. /* SetEvent sets all event gadgets */
  286. void SetEvent(void) {
  287.  
  288.   /* Set all gadgets to new values */
  289.   SetWDay(wday); SetDay(day); SetMonth(month); SetYear(year);
  290.   ShowWDay();
  291.   SetBefore(before); SetAfter(after);
  292.   SetAutodelete(autodelete); SetGrouped(grouped);
  293.   SetText(text);
  294.   SetPort(arexxport); SetCom(arexxcom); SetAMode(mode);
  295. }
  296.  
  297. /* ShowWDay checks the event being edited and if it is not a repeating one
  298.    and Weekday is ANY, shows the weekday of the event in the ShowWDay gadget */
  299. void ShowWDay(void) {
  300.  
  301.   struct Gadget *gad = MainGadgets[GDX_ShowWDay];
  302.  
  303.   /* Check if ShowWDay gadget shouldn't display a weekday */
  304.   if (day == 0 || month == 0 || year == 0 || wday != 0) {
  305.     /* If ShowWDay gadget shows a weekday, remove it */
  306.     if (swdaytxt != NULL) {
  307.       swdaytxt = NULL;
  308.       GT_SetGadgetAttrs(gad, MainWnd, NULL, GTTX_Text, (Tag)NULL, TAG_DONE);
  309.     }
  310.   }
  311.   else {
  312.     /* Otherwise calculate the new weekday */
  313.     swdaytxt = wdayabbrv+4*weekday(day, month, year);
  314.     /* Show value in gadget */
  315.     GT_SetGadgetAttrs(gad, MainWnd, NULL, GTTX_Text, (Tag)swdaytxt, TAG_DONE);
  316.   }
  317. }
  318.  
  319. /* PressButton simulates visually the pressing of a boolean button */
  320. void PressButton(struct Gadget *gad) {
  321.  
  322.   /* Display the gadget as selected */
  323.   RemoveGList(MainWnd, gad, 1);    /* Remove the gadget from window */
  324.   gad->Flags |= GFLG_SELECTED;
  325.   AddGList(MainWnd, gad, ~0, 1, NULL); /* Put gadget back to window */
  326.   RefreshGList(gad, MainWnd, NULL, 1); /* Refresh the gadget */
  327.   /* Wait for 1/4 seconds */
  328.   Delay(TICKS_PER_SECOND/4);
  329.   /* Display the gadget as not selected */
  330.   RemoveGList(MainWnd, gad, 1);    /* Remove the gadget from window */
  331.   gad->Flags &= ~GFLG_SELECTED;
  332.   AddGList(MainWnd, gad, ~0, 1, NULL); /* Put gadget back to window */
  333.   RefreshGList(gad, MainWnd, NULL, 1); /* Refresh the gadget */
  334. }
  335.  
  336. /* ResetEvent deselects the eventlist event */
  337. void ResetEvent(void) {
  338.  
  339.   struct Gadget *gad = MainGadgets[GDX_Eventlist];
  340.  
  341.   GT_SetGadgetAttrs(gad, MainWnd, NULL, GTLV_Selected, (Tag)~0, TAG_DONE);
  342. }
  343.  
  344. /* SetEventlist sets the Eventlist gadget to given value */
  345. void SetEventlist(int newval) {
  346.  
  347.   struct Gadget *gad = MainGadgets[GDX_Eventlist];
  348.  
  349.   /* Update the selection and top line of the Eventlist gadget */
  350.   GT_SetGadgetAttrs(gad, MainWnd, NULL, GTLV_Selected, (Tag)newval,
  351.             GTLV_Top, (Tag)newval, TAG_DONE);
  352. }
  353.  
  354. /* DetachEventlist detached the event list from the Eventlist gadget */
  355. void DetachEventlist(void) {
  356.  
  357.   struct Gadget *gad = MainGadgets[GDX_Eventlist];
  358.  
  359.   GT_SetGadgetAttrs(gad, MainWnd, NULL, GTLV_Labels, (Tag)~0, TAG_DONE);
  360. }
  361.  
  362. /* AttachEventlist attaches the given list to Eventlist gadget */
  363. void AttachEventlist(struct List *list) {
  364.  
  365.   struct Gadget *gad = MainGadgets[GDX_Eventlist];
  366.  
  367.   GT_SetGadgetAttrs(gad, MainWnd, NULL, GTLV_Labels, (Tag)list,
  368.             GTLV_Selected, (Tag)~0, TAG_DONE);
  369. }
  370.  
  371. /* matchstr checks if the first string is a prefix of the second */
  372. static int matchstr(const char *matched, const char *str) {
  373.  
  374.   for (; *matched != '\0'; matched++, str++) {
  375.     /* If the other string ends, no match */
  376.     if (*str == '\0')
  377.       return 0;
  378.     
  379.     /* If the strings have different characters, no match */
  380.     if (toupper(*matched) != toupper(*str))
  381.       return 0;
  382.   }
  383.  
  384.   /* If we are here, the strings matched */
  385.   return 1;
  386. }
  387.